/**
 * @author Nuno Pombo
 */

var ecra = new Array(
	new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1),
	new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1),
	new Array(1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 5, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1),
	new Array(1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1),
	new Array(1, 0, 1, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 0, 0, 0, 0, 1, 0, 1),
	new Array(1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1),
	new Array(1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 4, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1),
	new Array(1, 0, 1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1, 0, 1),
	new Array(1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1),
	new Array(1, 1, 1, 0, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 1),
	new Array(1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1),
	new Array(1, 0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 1, 1, 0, 1),
	new Array(1, 0, 6, 1, 0, 0, 0, 0, 0, 0, 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
var ghostX = 12;
var ghostY =12;
var ghostStop = 0;
var imagesSize = 20;
var initPositionX = 40;
var initPositionY = 40;
var xMapSize = 25;
var yMapSize = 25;
var direction = 'L';
var timerPacman;
var food = 0;
var points = 0;
var lives = 1;

function getPosition(a){
	return initPositionX + a * imagesSize;
}

function drawImage(x, y, type){
	oImg = document.createElement('img');
	document.body.appendChild(oImg);
	switch(type){
		case 0:
			oImg.src = "images/food.png";
			food++;
			break;
		case 1:
			oImg.src = "images/wall.png";
			break;
		case 2:
			oImg.src = "images/black.png";
			break;
        case 3:
            oImg.src = "images/speed.png";
            break;
        case 4:
            oImg.src = "images/ghostFood.png";
            food++;
            break;
        case 5:
        case 6:
            oImg.src = "images/tunel.png";
            break;
	}
	oImg.style.position = 'absolute';
	oImg.style.top = getPosition(y);
	oImg.style.left = getPosition(x);
}

function drawMap(){
	for(i=0; i<yMapSize; i++){
		for(j=0; j<xMapSize; j++){
			drawImage(i, j, ecra[i][j]);
		}
	}
}

function init(){
    document.pontuacao.pts.value = points;
    document.pontuacao.fd.value = food;
	drawMap();
	drawPacman(pacX, pacY);
	drawGhost(ghostX, ghostY);
	movePacman();
    moveGhost();
}

function drawPacman(x, y){
    document.pontuacao.pts.value = points;
    document.pontuacao.fd.value = food;
	pacman.style.top = getPosition(x);
	pacman.style.left = getPosition(y);
}

function drawGhost(x, y){
	ghost.style.top = getPosition(x);
	ghost.style.left = getPosition(y);
}

function changeDirection(event){
	switch(event.keyCode){
		case 37:
			direction = 'L';
			pacman.src = "images/pacmanL.gif";
			break;
		case 38:
			direction = 'U';
			pacman.src = "images/pacmanU.gif";
			break;
		case 39:
			direction = 'R';
			pacman.src = "images/pacmanR.gif";
			break;
		case 40:
			direction = 'D';
			pacman.src = "images/pacmanD.gif";
			break;
	}
}

function movePacman(){
	switch(direction){
		case 'L':
			if(ecra[pacY-1][pacX] != 1)
				pacY -= 1;
			break;
		case 'U':
			if(ecra[pacY][pacX-1] != 1)
				pacX -= 1;
			break;
		case 'R':
			if(ecra[pacY+1][pacX] != 1)
				pacY += 1;
			break;
		case 'D':
			if(ecra[pacY][pacX+1] != 1)
				pacX += 1;
			break;
	}
	//verificações
	drawPacman(pacX, pacY);
    
	if(ecra[pacY][pacX] == 0){
		drawImage(pacY, pacX, 2);
        ecra[pacY][pacX] = 2;
		food--;
		points += 10;
        if(food == 0){
            alert("GANHOU o Jogo!!!\nPrima F5 para Novo Jogo.");
        }
	}
    
    if(ecra[pacY][pacX] == 4){
        drawImage(pacY, pacX, 2);
        ecra[pacY][pacX] = 2;
        food--;
        points += 5;
        ghostStop = 1;
        ghost.src = "images/ghost_stop.gif";
        if(food == 0){
            alert("GANHOU o Jogo!!!\nPrima F5 para Novo Jogo.");
        }
    }
    
    if(pacX == ghostX && pacY == ghostY){
        if(ghostStop == 0){
            alert("PERDEU o Jogo!!!\nPrima F5 para Novo Jogo.");
            lives--;
        }else{
            points += 1000;
            ghostX = ghostY = 12;
            ghostStop = 0;
            ghost.src = "images/ghost.gif";
            drawGhost(ghostX, ghostY);
        }
    }
    
    if(ecra[pacY][pacX] >= 5){
        var numTunel = ecra[pacY][pacX];
        var auxX = pacX;
        var auxY = pacY;
        for(i=0; i<yMapSize; i++){
            for(j=0; j<xMapSize; j++){
                if(ecra[i][j] == numTunel){
                    if(i!=auxY || j!=auxX){
                        pacY = i;
                        pacX = j;
                        break;
                    }
                }
            }
        }
    }
	
    if(lives!=0 && food!=0){
    	if(ecra[pacY][pacX] == 3){
    	   timerPacman = window.setTimeout("movePacman()", 75);
    	}else{
        	timerPacman = window.setTimeout("movePacman()", 300);
        }
    }
}

function moveGhost(){
    var quadrante;
    var not = 0;
    
    if(pacX < ghostX && pacY < ghostY)
        quadrante = 1;
    else if(pacX < ghostX && pacY > ghostY)
        quadrante = 2;
    else if(pacX > ghostX && pacY < ghostY)
        quadrante = 3;
    else if(pacX > ghostX && pacY > ghostY)
        quadrante = 4;
    else
        quadrante = 0;
        
    switch(quadrante){
        case 1:
            if(ecra[ghostY-1][ghostX] != 1){
                ghostY -=1;
                if(not == 1)
                    not = 0;
            }else if(ecra[ghostY][ghostX-1] != 1){
                if(not != 1)
                    ghostX -=1;
                else
                    ghostX +=1;
            }else{
                ghostX +=1;
                not = 1;
            }
            break;
        case 2:
            if(ecra[ghostY+1][ghostX] != 1){
                ghostY +=1;
                if(not == 1)
                    not = 0;
            }else if(ecra[ghostY][ghostX-1] != 1){
                if(not != 1)
                    ghostX -=1;
                else
                    ghostX +=1;
            }else{
                ghostX +=1;
                not = 1;
            }
            break;
        case 3:
            if(ecra[ghostY-1][ghostX] != 1){
                ghostY -=1;
                if(not == 1)
                    not = 0;
            }else if(ecra[ghostY][ghostX+1] != 1){
                if(not != 1)
                    ghostX +=1;
                else
                    ghostX -=1;
            }else{
                ghostX -=1;
                not = 1;
            }
            break;
        case 4:
            if(ecra[ghostY+1][ghostX] != 1){
                ghostY +=1;
                if(not == 1)
                    not = 0;
            }else if(ecra[ghostY][ghostX+1] != 1){
                if(not != 1)
                    ghostX +=1;
                else
                    ghostX -=1;
            }else{
                ghostX -=1;
                not = 1;
            }
            break;
        case 0:
            if(pacX == ghostX){
                if(pacY < ghostY && ecra[ghostY-1][ghostX] != 1){
                    ghostY -=1;
                }else if(pacY > ghostY && ecra[ghostY+1][ghostX] != 1){
                    ghostY +=1;
                }
            }else if(pacY == ghostY){
                if(pacX < ghostX && ecra[ghostY][ghostX-1] != 1){
                    ghostX -=1;
                }else if(pacX > ghostX && ecra[ghostY][ghostX+1] != 1){
                    ghostX +=1;
                }
            }
            break;
    }
    
    if(lives != 0){
        drawGhost(ghostX, ghostY);
        timerGhost = window.setTimeout("moveGhost()", 320);
    }
}
